﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;
using DCT.CodeBox.Log4netWrapper.Codes;
using log4net;
using System.Collections.Generic;
using System.Dynamic;
using DCT.CodeBox.LightLog.Codes.Formatter;

namespace DCT.CodeBox.Log4netWrapper
{
    class Program
    {
        /// <summary>
        /// 例子
        /// </summary>
        class Log4netSample1 {
            private static Codes.Log4netWrapper wrapper = null;
            static Log4netSample1()
            {
                wrapper = new Codes.Log4netWrapper();
                wrapper.SetInitialConfig(Codes.Log4netWrapper.InitialConfigFromFile("logConfig.xml"));
                //还可以用直接用xml初始化
//              wrapper.SetInitialConfig(Codes.Log4netWrapper.InitialConfigFromXml(@"
//<log4net>
//    <appender name=""RollingLogFileAppender"" type=""log4net.Appender.RollingFileAppender"">
//        <lockingModel type=""log4net.Appender.FileAppender+MinimalLock"" />
//        <Encoding value=""UTF-8"" />
//        <file value=""log/logfile"" />            
//        <rollingStyle value=""Date"" />
//        <datePattern value=""yyyyMMdd&quot;.log&quot;"" />
//        <StaticLogFileName value=""false"" />
//        <layout type=""log4net.Layout.PatternLayout"">
//            <conversionPattern value=""[%date{yyyy-MM-dd HH:mm:ss}] [%level] %message %exception %newline"" />
//        </layout>
//    </appender>
//    <logger name=""myLogger"">
//        <level value=""All""/>
//        <appender-ref ref=""RollingLogFileAppender""/>
//    </logger>
//</log4net>
//"));
                wrapper.DefaultLoggerName = "myLogger";
            }

            public static ILog Log {
                get {
                    return wrapper.GetLogger();
                }
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("======测试log");
            Log4netSample1.Log.Debug("debug log");
            Log4netSample1.Log.Error("Error log");
            Log4netSample1.Log.Fatal("Fatal log");
            Log4netSample1.Log.Info("Info log");

            Console.WriteLine("======测试log结束");

            Console.WriteLine("======测试计时log");

            //设置计时默认日志：全局设置一次即可
            DurationLogger.DefaultLogAction = new Action<DurationLoggerContext>(
            delegate(DurationLoggerContext context)
            {
                string msg = context.Name + "[" + context.Status.ToString() + "]:" + context.Log
                    + "|运行时间：" + context.Elasped.Seconds + "秒";
                if (context.Elasped.Seconds > 1)
                {
                    //超时警告
                    Log4netSample1.Log.Error(msg);
                }
                else
                {
                    Log4netSample1.Log.Info(msg);
                }
            });

            //记录日志，用using，保证可以dispose
            using (DurationLogger dl = DurationLogger.Create("test"))
            {
                dl.Start("开始执行");
                Thread.Sleep(2000); //模拟某个耗时为2s的操作
                dl.Log("过程一");
                dl.Restart();       //分别计算“过程一”、“过程二”的时间，如果不需要分别计算，此步骤不需要执行
                Thread.Sleep(1000); //模拟某个耗时为1s的操作
                dl.Log("过程二");
            }

            Console.WriteLine("======测试计时log结束");

            Console.WriteLine("======测试LogStringFormatter");

            //一般类型
            Log4netSample1.Log.Info(LogStringFormatter.Render(1));
            Log4netSample1.Log.Info(LogStringFormatter.Render(0.1));
            Log4netSample1.Log.Info(LogStringFormatter.Render(true));
            Log4netSample1.Log.Info(LogStringFormatter.Render("这是一个字符串"));

            //实体Object
            Log4netSample1.Log.Info(LogStringFormatter.Render(new { 
                Name = "Peter", Age = 29
            }));

            //Dictionary
            Log4netSample1.Log.Info(LogStringFormatter.Render(new Dictionary<string, object>() { 
                {"Task1",    "需求分析"},
                {"Task2",    "概要设计"},
                {"Task3",    "编码测试"}
            }));

            //expandoObject
            dynamic obj = new ExpandoObject();
            obj.Name = "Anna";
            obj.Position = "Project Manager";
            Log4netSample1.Log.Info(LogStringFormatter.Render(obj));

            Console.WriteLine("======测试LogStringFormatter结束");

            //打开exe所在文件夹
            OpenExecuteFolder();
        }

        private static void OpenExecuteFolder() {
            string binfile = Assembly.GetExecutingAssembly().Location;
            string binfolder = Path.GetDirectoryName(binfile);
            System.Diagnostics.Process.Start("explorer.exe", binfolder);
        }
    }
}
